/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.service;

import java.io.Serializable;
import java.util.ArrayList;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


import org.independent.servlet.session.ServletSession;

/**
 * the context for ThreadLocal
 * 
 * @author lcj
 * 
 */
public class Context implements Serializable {
	private static final long serialVersionUID = 1L;
	private ServletRequest request;
	private ServletResponse response;
	private ServletSession session = null;
	private int hasSession = 0;
	private int sessionIdCommitType = 0;

	private int markObjectClass = 0;
	private ArrayList<Class<?>> objectClass = new ArrayList<Class<?>>();

	public void setMarkObjectClass(int s) {
		if (markObjectClass == 0)
			this.markObjectClass = s;
	}

	public void clearMarkObjectClass() {
		this.markObjectClass = 0;
	}

	public void addObjectClasses(Class<?> clazz) {
		if (this.markObjectClass == 0) {
			objectClass.add(clazz);
		}
	}

	public boolean contains(Class<?> clazz) {
		return objectClass.contains(clazz);
	}

	public int getSessionIdCommitType() {
		return sessionIdCommitType;
	}

	public void setSessionIdCommitType(int sessionIdCommitType) {
		this.sessionIdCommitType = sessionIdCommitType;
	}

	/**
	 * get the information of security about session's id
	 * 
	 * @return
	 */
	final public String getSessionMark() {
		String ip = ((HttpServletRequest) request).getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = ((HttpServletRequest) request).getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = ((HttpServletRequest) request).getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		int position = ip.indexOf(",");
		if (position != -1) {
			ip = ip.substring(0, position);
		}
		return ip;
	}

	public int getHasSession() {
		return hasSession;
	}

	public void setHasSession(int hasSession) {
		this.hasSession = hasSession;
	}

	public ServletRequest getRequest() {
		return request;
	}

	public void setRequest(ServletRequest request) {
		this.request = request;
	}

	public ServletResponse getResponse() {
		return response;
	}

	public void setResponse(ServletResponse response) {
		this.response = response;
	}

	public void setSession(ServletSession session) {
		this.session = session;
	}

	public ServletSession getSession() {
		return session;
	}

}
